package com.lf.controller;


import com.lf.constant.ConstantsValue;
import com.lf.entity.ResponseWrapper;
import com.lf.enums.ErrorCodeEnum;
import com.lf.exception.ApiException;
import com.lf.service.ServiceList;
import com.lf.swagger.ApiJsonObject;
import com.lf.swagger.ApiJsonProperty;
import com.lf.util.Asserts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @program: 接口【文件操作】
 * @description: 会员
 * @author: gxl
 * @create:
 **/
@Slf4j
@Api(value = "file", tags = "文件操作")
@RestController
@RequestMapping("/file")
public class FileController extends ServiceList {

    /**
     * http上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
    @ApiOperation(tags = "文件操作", httpMethod = "POST", value = "[前端]上传图片", response = String.class)
    public ResponseWrapper uplaodMainPicture(@ApiJsonObject(name = "uploadPictureParams", value = {
            @ApiJsonProperty(key = "file", value = "图片")
    }) @RequestParam("file") MultipartFile file) {
        //1. 接受上传的文件  @RequestParam("file") MultipartFile file
        try {
            return userService.uploadPicture(file);
        } catch (ApiException e) {
            log.error(String.format("[后台]上传图片失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        } catch (Exception e) {
            log.error(String.format("[后台]上传图片失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    /**
     * 身份证正面照片上传
     * @param file
     * @param
     * @return
     */
    @RequestMapping(value = "/uploadIdCard", method = RequestMethod.POST)
    @ApiOperation(tags = "文件操作", httpMethod = "POST", value = "[前端]身份证正面照片上传", response = String.class)
    public ResponseWrapper uploadIdCard(@ApiJsonObject(name = "uploadIdCardParams", value = {
            @ApiJsonProperty(key = "file", value = "图片"),
            @ApiJsonProperty(key = "userId", value = "用户ID")
    }) @RequestParam("file") MultipartFile file, @RequestParam("userId") Integer userId) {
        //1. 接受上传的文件  @RequestParam("file") MultipartFile file
        try {
            Asserts.checkUserId(userId,"用户ID");
            return userService.uploadIdCard(file,userId);
        } catch (ApiException e) {
            log.error(String.format("[后台]上传图片失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        } catch (Exception e) {
            log.error(String.format("[后台]上传图片失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    /**
     * ftp文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    @ApiOperation(tags = "文件操作", httpMethod = "POST", value = "[前端]上传图片", response = String.class)
    public String fileUpload(@ApiJsonObject(name = "fileUploadParams", value = {
            @ApiJsonProperty(key = "file", value = "图片")
    }) @RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception{
        String fileName = ocrService.fileUpload(file);
        if (fileName == null){
            return "上传失败！！";
        }
        return "上传成功！！";
    }

    /**
     * 更新文本
     * @return
     */
    @RequestMapping(value = "updateText", method = RequestMethod.GET)
    @ApiOperation(tags = "文本操作", httpMethod = "GET", value = "[测试]更新文本", response = String.class)
    public ResponseWrapper updateText(@ApiJsonObject(name = "updateTextParams", value = {})  HttpServletRequest request){
        try {
            redisLockRegistry.obtain(ConstantsValue.COLONY_DRAWN_LOCK).lock();
            ConstantsValue.DRAWN_LOCK.writeLock().lock();
            return ocrService.updateText();
        } catch (ApiException e) {
            log.error(String.format("[测试]更新文本失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        } catch (Exception e) {
            log.error(String.format("[测试]更新文本失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }finally {
            redisLockRegistry.obtain(ConstantsValue.COLONY_DRAWN_LOCK).unlock();
            ConstantsValue.DRAWN_LOCK.writeLock().unlock();
        }
    }

    /**
     * 获取文本
     * @return
     */
    @RequestMapping(value = "getText", method = RequestMethod.GET)
    @ApiOperation(tags = "文本操作", httpMethod = "GET", value = "[测试]获取文本", response = String.class)
    public ResponseWrapper getText(@ApiJsonObject(name = "getTextParams", value = {}) HttpServletRequest request){
        try {
            redisLockRegistry.obtain(ConstantsValue.COLONY_DRAWN_LOCK).lock();
            ConstantsValue.DRAWN_LOCK.readLock().lock();
            return ocrService.getText();
        } catch (ApiException e) {
            log.error(String.format("[测试]获取文本失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        } catch (Exception e) {
            log.error(String.format("[测试]获取文本失败，参数:%s, 错误信息:%s", "", e.getMessage()));
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        } finally {
            redisLockRegistry.obtain(ConstantsValue.COLONY_DRAWN_LOCK).unlock();
            ConstantsValue.DRAWN_LOCK.readLock().unlock();
        }
    }

}
